******************************************************************************
*** 00_master.do *********************************************************
*
* Goal of file: 	MASTER FILE: set globals and load do-files
*
* Code:				Luke Haywood (lh) & Markus Janser (mj)
*
*** last change: 2023/05/01 (lh)************************************************
********************************************************************************

*** TABULA RASA ***
clear all

*** VERSION ***
version 15

*** SETTINGS ***
set more off 
set linesize 200        
set rmsg on            
set maxvar 32767, permanently
set excelxlsxlargefile on 

*** FILENAME & FILE-SPECIFIC GLOBALS ***
glo filename 	master_v01
glo date 		= string( d(`c(current_date)'), "%tdCYND" )
glo log_active (1) 			// Aktiviert Log-File zu Beginn des Do-Files und schließt das Log-File am Ende (0 = no, 1 = yes)
glo crossf_globals (1) 
glo start_dofiles (0)
glo iab (1)

*** PATHS ***
glo user = 1								 // 1 -> mj; 2-> user 2; else -> cooperation partners etc.
if ${user} == 1 {
	global main  = " (LOCATION OF FILES - deleted for JAERE SUBMISSION - LH) "
	global prog  = "${main}\prog"			 // Store program files in '${prog}'
	global ado   = "${prog}\ado"			 // Store ado files in subfolder of '${prog}'
	global orig  = "${main}\orig"			 // Store original data files in '${orig}'
	global data  = "${main}\data"		 // Store modified data files in '${data}'
	global log   = "${main}\log"			 // Store logfiles in '${log}'
	global results = "${main}\results"		 // Store results in '${results}'
	global graphs = "${main}\results\graphs" // Store graphs in subfolder of '${log}'
	}		
else if ${user} == 2 {
	* Path definitions of user 2
}

cd ${main}
adopath++ ${ado}

*** START LOG-FILE ***
if ${log_active}==1 {
	mac list date
	capture log close
	log using ${log}/${filename}_${date}.log, replace text
	}
	
*
* CROSS-FILE GLOBALS
*-------------------------------------------------------------------------------
if ${crossf_globals}==1 {

	* a) Header and general global
	
* CROSS-FILE GLOBALS
*------------------------------------------------------------------------------*

	* a) Header and general global
	
	global iab = 1 
	
	global site = "b" /*There are five possibilities: "g" for germany/"l" for lausitz/"m" for mittel/"b" for lausitz and mittel/"r" for rhein*/
	
	global region = 1 /*There are two possibilities, you can use "0" for kreise or "1" for gemeinden */

	* globals for treatment of short duration spells	
	global dur_short = 7

	* globals for closing black holes by prolonging other labour market spells
	global black_hole_gap_month = 28
	global black_hole_gap_3month = 92
	* globals for criteria for keeping employment biographies as function of number / fraction of black holes
	global bhcritpct = 100/100  /* individuals with more than X% of spells in black hole are removed from sample */ 
	global bhcritnr = 20      /* individuals with more than X \in (1,20) black hole spells are removed from the sample */
	global bhcritspellnr=1 	/* we separate employment biographies which are cut by long black holes - keep only the first X of these spells*/	
	global bhcritseries=1	// we separate employment biographies which are cut by long black holes and keep only X series
	global interval = 30.4375 /*This global determines the intervals for the wage analysis. "30.4375" means
						that the intervals are 1 month long. */
	
	global incmax=8000  /* used in 5estimations to censor (real) wages above this monthly level as measur't error*/

*	TIME GLOBALS & VARIABLES: Create cutoff date variable: June 30, 2017
*------------------------------------------------------------------------------*

global maxyear = 2017         // last wave of sample used in estimation

global cdate mdy(6, 30, 2017) // cut-off-date for descriptives 
	
	****************************************************************
	* Globals for the margemp extra earnings (margempee) threshold *
	****************************************************************
		/* This global defines the threshold income until which a Dazuverdiener 
		is treated as being unemployed. Above the threshold a margemp status is 
		recognized as such. */

		global margempee_1975 = 0 	
		global margempee_1976 = 0 	
		global margempee_1977 = 111 			
		global margempee_1978 = 132 	
		global margempee_1979 = 126 	
		global margempee_1980 = 132 	
		global margempee_1981 = 140 	
		global margempee_1982 = 147.5 			
		global margempee_1983 = 154.5 	
		global margempee_1984 = 163.5 	
		global margempee_1985 = 168 	
		global margempee_1986 = 172 	
		global margempee_1987 = 180.5 			
		global margempee_1988 = 184.5 	
		global margempee_1989 = 189 	
		global margempee_1990 = 197 	
		global margempee_1991 = 201.5 	
		global margempee_1992 = 210 			
		global margempee_1993 = 222.5 	
		global margempee_1994 = 235 	
		global margempee_1995 = 243.5 	
		global margempee_1996 = 247.5 	
		global margempee_1997 = 256 			
		global margempee_1998 = 260 	
		global margempee_1999 = 157.5 	
		global margempee_2000 = 157.5 	
		global margempee_2001 = 165 	
		global margempee_2002 = 165 			
		global margempee_2003 = 165 	
		global margempee_2004 = 165 	
		global margempee_2005 = 165 	
		global margempee_2006 = 165 			
		global margempee_2007 = 165 	
		global margempee_2008 = 165 	
		global margempee_2009 = 165 	
		global margempee_2010 = 165 	
		global margempee_2011 = 165 			
		global margempee_2012 = 165 	
		global margempee_2013 = 165 	
		global margempee_2014 = 165 	
		global margempee_2015 = 165 			
		global margempee_2016 = 165 	
		global margempee_2017 = 165 	
		global margempee_2018 = 165 	
		
	****************************************************************
	* Globals for the margemp maximum earnings (margempme) threshold *
	****************************************************************

		global margempme_1975 = 185	
		global margempme_1976 = 185	
		global margempme_1977 = 195 			
		global margempme_1978 = 195 	
		global margempme_1979 = 195 	
		global margempme_1980 = 195 	
		global margempme_1981 = 195
		global margempme_1982 = 195
		global margempme_1983 = 195
		global margempme_1984 = 195 	
		global margempme_1985 = 195 	
		global margempme_1986 = 200 	
		global margempme_1987 = 205
		global margempme_1988 = 215 	
		global margempme_1989 = 220 	
		global margempme_1990 = 235 	
		global margempme_1991 = 240 	
		global margempme_1992 = 250 			
		global margempme_1993 = 265
		global margempme_1994 = 280 	
		global margempme_1995 = 290 	
		global margempme_1996 = 295
		global margempme_1997 = 310 			
		global margempme_1998 = 310 	
		global margempme_1999 = 315 	
		global margempme_2000 = 315 	
		global margempme_2001 = 315 	
		global margempme_2002 = 325 			
		global margempme_2003 = 375 	
		global margempme_2004 = 400 	
		global margempme_2005 = 400 	
		global margempme_2006 = 400 			
		global margempme_2007 = 400 	
		global margempme_2008 = 400 	
		global margempme_2009 = 400 	
		global margempme_2010 = 400 	
		global margempme_2011 = 400 			
		global margempme_2012 = 400 	
		global margempme_2013 = 450 	 	
		global margempme_2014 = 450 	 	
		global margempme_2015 = 450 				
		global margempme_2016 = 450 	
		global margempme_2017 = 450 	 	
		global margempme_2018 = 450 	
		
	* Global for defining the maximum length of small overlaps		
	global small_overlaps = 5 /* This global defines the maximum tolerated length of small overlaps.*/
		
	****************************************************************
	* Globals for the unemployment benefit						   *
	****************************************************************
		
		global unempben_2005_east = 331
		global unempben_2005_west = 345
			// These globals were always in effect from July 1!
			global unempben_2006_east = 345 
			global unempben_2006_west = 345 
			global unempben_2007_east = 347
			global unempben_2007_west = 347
			global unempben_2008_east = 351
			global unempben_2008_west = 351
			global unempben_2009_east = 359
			global unempben_2009_west = 359
		global unempben_2011_east = 364
		global unempben_2011_west = 364
		global unempben_2012_east = 374
		global unempben_2012_west = 374
		global unempben_2013_east = 382
		global unempben_2013_west = 382
		global unempben_2014_east = 391
		global unempben_2014_west = 391
		global unempben_2015_east = 399
		global unempben_2015_west = 399
		global unempben_2016_east = 404
		global unempben_2016_west = 404
		global unempben_2017_east = 409
		global unempben_2017_west = 409
		global unempben_2018_east = 416
		global unempben_2018_west = 416
		
	****************************************************************
	* Globals for the housing benefit						   *
	****************************************************************
	/* Note the coding for the states:
		1 - Schleswig-Holstein
		2 - Hamburg
		3 - Niedersachsen
		4 - Bremen
		5 - NRW
		6 - Hessen
		7 - Rheinland-Pfalz
		8 - Baden-Württemberg
		9 - Bayern
		10 - Saarland
		11 - Berlin
		12 - Brandenburg
		13 - Mecklenburg-Vorpommern
		14 - Sachsen
		15 - Sachsen-Anhalt
		16 - Thüringen */	
		
		global housben_2001_1 = 64.4
		global housben_2002_1 = 65.77
		global housben_2003_1 = 66.55
		global housben_2004_1 = 68.87
		global housben_2005_1 = 41.27
		global housben_2006_1 = 41.43
		global housben_2007_1 = 40.20
		global housben_2008_1 = 40.52
		global housben_2009_1 = 59.73
		global housben_2010_1 = 61.64
		global housben_2011_1 = 54.98
		global housben_2012_1 = 51.68
		global housben_2013_1 = 50.87
		global housben_2014_1 = 50.11
		global housben_2015_1 = 49.42
		global housben_2016_1 = 72.50
		global housben_2017_1 = 72.50
		
		global housben_2001_2 = 77.41															
		global housben_2002_2 = 77.53
		global housben_2003_2 = 78.35
		global housben_2004_2 = 79.11
		global housben_2005_2 = 47.44
		global housben_2006_2 = 45.22
		global housben_2007_2 = 43.59
		global housben_2008_2 = 42.30
		global housben_2009_2 = 61.37
		global housben_2010_2 = 61.67
		global housben_2011_2 = 55.99
		global housben_2012_2 = 54.87
		global housben_2013_2 = 53.66
		global housben_2014_2 = 52.85
		global housben_2015_2 = 52.24
		global housben_2016_2 = 78.55
		global housben_2017_2 = 78.55
		
		global housben_2001_3 = 58.11													
		global housben_2002_3 = 60.78
		global housben_2003_3 = 62.20
		global housben_2004_3 = 62.61
		global housben_2005_3 = 38.97
		global housben_2006_3 = 37.85
		global housben_2007_3 = 37.48
		global housben_2008_3 = 37.62
		global housben_2009_3 = 56.12	
		global housben_2010_3 = 58.32
		global housben_2011_3 = 52.03
		global housben_2012_3 = 48.86
		global housben_2013_3 = 47.88
		global housben_2014_3 = 47.27	
		global housben_2015_3 = 45.99
		global housben_2016_3 = 68.28
		global housben_2017_3 = 68.28
		
		global housben_2001_4 = 68.10															
		global housben_2002_4 = 68.31
		global housben_2003_4 = 69.08
		global housben_2004_4 = 69.57
		global housben_2005_4 = 42.08
		global housben_2006_4 = 41.78
		global housben_2007_4 = 38.01
		global housben_2008_4 = 40.28
		global housben_2009_4 = 58.83
		global housben_2010_4 = 62.60
		global housben_2011_4 = 56.86
		global housben_2012_4 = 55.15
		global housben_2013_4 = 54.47
		global housben_2014_4 = 54.86
		global housben_2015_4 = 53.81
		global housben_2016_4 = 78.24
		global housben_2017_4 = 78.24
		
		global housben_2001_5 = 59.45															
		global housben_2002_5 = 61.87
		global housben_2003_5 = 63.58
		global housben_2004_5 = 64.35
		global housben_2005_5 = 39.63
		global housben_2006_5 = 38.79
		global housben_2007_5 = 38.74
		global housben_2008_5 = 38.72
		global housben_2009_5 = 57.41
		global housben_2010_5 = 59.31
		global housben_2011_5 = 52.32
		global housben_2012_5 = 50.07
		global housben_2013_5 = 48.75
		global housben_2014_5 = 48.36
		global housben_2015_5 = 47.02
		global housben_2016_5 = 70.32
		global housben_2017_5 = 70.32
		
		global housben_2001_6 = 61.80															
		global housben_2002_6 = 64.07
		global housben_2003_6 = 65.27
		global housben_2004_6 = 71.94
		global housben_2005_6 = 40.64
		global housben_2006_6 = 39.43
		global housben_2007_6 = 39.13
		global housben_2008_6 = 38.22
		global housben_2009_6 = 57.33
		global housben_2010_6 = 58.64
		global housben_2011_6 = 51.64
		global housben_2012_6 = 51.00
		global housben_2013_6 = 51.47
		global housben_2014_6 = 51.62
		global housben_2015_6 = 49.76
		global housben_2016_6 = 75.41
		global housben_2017_6 = 75.41
		
		global housben_2001_7 = 50.57															
		global housben_2002_7 = 53.51
		global housben_2003_7 = 55.59
		global housben_2004_7 = 56.08
		global housben_2005_7 = 38.94
		global housben_2006_7 = 38.13
		global housben_2007_7 = 37.56
		global housben_2008_7 = 36.78
		global housben_2009_7 = 56.07
		global housben_2010_7 = 57.89
		global housben_2011_7 = 52.22
		global housben_2012_7 = 50.83
		global housben_2013_7 = 49.95
		global housben_2014_7 = 49.01
		global housben_2015_7 = 47.69
		global housben_2016_7 = 70.46
		global housben_2017_7 = 70.46
		
		global housben_2001_8 = 57.82															
		global housben_2002_8 = 60.15
		global housben_2003_8 = 60.83
		global housben_2004_8 = 62.23
		global housben_2005_8 = 40.54
		global housben_2006_8 = 39.99
		global housben_2007_8 = 40.44
		global housben_2008_8 = 39.75
		global housben_2009_8 = 63.43
		global housben_2010_8 = 65.06
		global housben_2011_8 = 58.94
		global housben_2012_8 = 57.93
		global housben_2013_8 = 57.09
		global housben_2014_8 = 56.98
		global housben_2015_8 = 53.75
		global housben_2016_8 = 79.93
		global housben_2017_8 = 79.93
		
		global housben_2001_9 = 60.23															
		global housben_2002_9 = 61.60
		global housben_2003_9 = 63.31
		global housben_2004_9 = 64.47
		global housben_2005_9 = 38.64
		global housben_2006_9 = 37.70
		global housben_2007_9 = 36.63
		global housben_2008_9 = 36.52
		global housben_2009_9 = 57.41
		global housben_2010_9 = 58.82
		global housben_2011_9 = 51.26
		global housben_2012_9 = 48.93
		global housben_2013_9 = 47.52
		global housben_2014_9 = 46.52
		global housben_2015_9 = 45.00
		global housben_2016_9 = 67.99
		global housben_2017_9 = 67.99
		
		global housben_2001_10 = 63.81															
		global housben_2002_10 = 63.26
		global housben_2003_10 = 61.16
		global housben_2004_10 = 44.58
		global housben_2005_10 = 40.31
		global housben_2006_10 = 38.82
		global housben_2007_10 = 38.06
		global housben_2008_10 = 40.74
		global housben_2009_10 = 58.47
		global housben_2010_10 = 61.10
		global housben_2011_10 = 50.67
		global housben_2012_10 = 46.99
		global housben_2013_10 = 46.27
		global housben_2014_10 = 44.30
		global housben_2015_10 = 42.96
		global housben_2016_10 = 65.16
		global housben_2017_10 = 65.16
		
		global housben_2001_11 = 63.87														
		global housben_2002_11 = 71.84
		global housben_2003_11 = 71.96
		global housben_2004_11 = 73.24
		global housben_2005_11 = 50.50	
		global housben_2006_11 = 49.15
		global housben_2007_11 = 47.70
		global housben_2008_11 = 45.52
		global housben_2009_11 = 67.00
		global housben_2010_11 = 69.76
		global housben_2011_11 = 61.92
		global housben_2012_11 = 62.03
		global housben_2013_11 = 61.31
		global housben_2014_11 = 60.67
		global housben_2015_11 = 59.25
		global housben_2016_11 = 83.60
		global housben_2017_11 = 83.60
		
		global housben_2001_12 = 49.29													
		global housben_2002_12 = 52.51	
		global housben_2003_12 = 54.27
		global housben_2004_12 = 55.94
		global housben_2005_12 = 47.18
		global housben_2006_12 = 46.82
		global housben_2007_12 = 46.32
		global housben_2008_12 = 44.05
		global housben_2009_12 = 63.10
		global housben_2010_12 = 66.23
		global housben_2011_12 = 58.73
		global housben_2012_12 = 56.69	
		global housben_2013_12 = 55.09
		global housben_2014_12 = 54.21
		global housben_2015_12 = 53.06
		global housben_2016_12 = 76.09
		global housben_2017_12 = 76.09
		
		global housben_2001_13 = 48.63											
		global housben_2002_13 = 51.07
		global housben_2003_13 = 52.91
		global housben_2004_13 = 54.36
		global housben_2005_13 = 45.58
		global housben_2006_13 = 44.76
		global housben_2007_13 = 44.73
		global housben_2008_13 = 43.42	
		global housben_2009_13 = 66.67
		global housben_2010_13 = 68.06
		global housben_2011_13 = 60.13	
		global housben_2012_13 = 55.76	
		global housben_2013_13 = 54.60	
		global housben_2014_13 = 52.77
		global housben_2015_13 = 50.64
		global housben_2016_13 = 72.21
		global housben_2017_13 = 72.21
		
		global housben_2001_14 = 48.32													
		global housben_2002_14 = 51.15
		global housben_2003_14 = 53.03
		global housben_2004_14 = 54.16
		global housben_2005_14 = 45.91
		global housben_2006_14 = 46.29
		global housben_2007_14 = 46.56
		global housben_2008_14 = 45.09	
		global housben_2009_14 = 62.23
		global housben_2010_14 = 64.95
		global housben_2011_14 = 57.12
		global housben_2012_14 = 54.56	
		global housben_2013_14 = 53.00
		global housben_2014_14 = 51.95
		global housben_2015_14 = 50.46
		global housben_2016_14 = 69.54
		global housben_2017_14 = 69.54
		
		global housben_2001_15 = 49.82															
		global housben_2002_15 = 51.14
		global housben_2003_15 = 52.60
		global housben_2004_15 = 53.53
		global housben_2005_15 = 42.26
		global housben_2006_15 = 41.84
		global housben_2007_15 = 40.21
		global housben_2008_15 = 40.81
		global housben_2009_15 = 65.24
		global housben_2010_15 = 67.69
		global housben_2011_15 = 59.04
		global housben_2012_15 = 53.96
		global housben_2013_15 = 52.37
		global housben_2014_15 = 51.02
		global housben_2015_15 = 49.59
		global housben_2016_15 = 71.17
		global housben_2017_15 = 71.17
		
		global housben_2001_16 = 48.86										
		global housben_2002_16 = 51.23	
		global housben_2003_16 = 52.90
		global housben_2004_16 = 54.50
		global housben_2005_16 = 46.70
		global housben_2006_16 = 46.48
		global housben_2007_16 = 46.14
		global housben_2008_16 = 45.60
		global housben_2009_16 = 64.60
		global housben_2010_16 = 66.69	
		global housben_2011_16 = 58.26
		global housben_2012_16 = 54.49
		global housben_2013_16 = 53.46	
		global housben_2014_16 = 52.15	
		global housben_2015_16 = 50.27	
		global housben_2016_16 = 71.44
		global housben_2017_16 = 71.44
	
	}
	**                **
** CLOSE LOG-FILE **	
if ${log_active}==1 {$
	capture log close
	}


***************************************
***  Launch data preparation **********
***************************************

noisily do ${prog}\1prepare.do

***************************************
***  Launch estimation files **********
***************************************

*global sample = 2 // sample explanations see above & readme
/*
di "Sample 7 for JAERE revision: sample with only post-2000 data (cells pre/post-2000)"
global sample = 7 // sample explanations see above & readme

di "Sample 8 for JAERE revision: cells by occupation"
global sample = 8 // sample explanations see above & readme

di "Sample 9 for JAERE revision: cells by region"
global sample = 9 // sample explanations see above & readme
*/

****************************************************************
di "Sample 2 - benchmark"
global sample = 2 // sample explanations see above & readme
** gen alphabetic folder names as numeric folders create problems in IAB server
if ${sample}==2{
global samplefolder = "two"
}
noisily do ${prog}\5estimations.do
noisily do ${prog}\7biocoal.do

****************************************************************
di "Sample 7 for JAERE revision: post-2000 data only"
global sample = 7 // sample=7 creates the sample demanded by JAERE referee 3
						* including only data post 2000
** gen alphabetic folder names as numeric folders create problems in IAB server
if ${sample}==7{
global samplefolder = "seven"
}
noisily do ${prog}\5estimations.do
noisily do ${prog}\7biocoal.do

****************************************************************
di "Sample 8 for JAERE revision: cells by occupation"
global sample = 8 // sample explanations see above & readme
** gen alphabetic folder names as numeric folders create problems in IAB server
if ${sample}==8{
global samplefolder = "eight"
}
noisily do ${prog}\5estimations.do
noisily do ${prog}\7biocoal.do

****************************************************************
di "Sample 9 for JAERE revision: cells by region"
global sample = 9 // sample explanations see above & readme
** gen alphabetic folder names as numeric folders create problems in IAB server
if ${sample}==9{
global samplefolder = "nine"
}
noisily do ${prog}\5estimations.do
noisily do ${prog}\7biocoal.do

clear
exit

